package com.woniu.p2p.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.p2p.entity.UserBank;
import com.woniu.p2p.service.MoneyManageService;
import com.woniu.p2p.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 86152
 * @create 2020/8/9
 * @since 1.0.0
 */
@RestController
@RequestMapping("/moneyManage")
public class moneyManageController {
    @Autowired
    MoneyManageService moneyManageService;

    /*获取账户余额*/
    @RequestMapping("/draw/showAmount")
    public Long showAmount(HttpServletRequest request){
        String token = request.getHeader("token");
        int userid = Integer.parseInt(token);
        Long amount=moneyManageService.findAmountById(userid);
        return amount;
    }

    /*支付宝提现*/
    @RequestMapping("/draw/zhifubaoDrawnow")
    public String drawnow(@RequestBody DrawNowVo drawNowVo, HttpServletRequest request){
        String token = request.getHeader("token");
        int userid = Integer.parseInt(token);
        /*通过userid查询支付密码*/
        String paypwd=moneyManageService.findPaypwdByUserid(userid);
        if(!drawNowVo.getPaypwd().equals(paypwd)){
            return "密码错误！";
        }else {
            Long drawmoney = drawNowVo.getDrawmoney();
            /*更新账户余额*/
            int i=moneyManageService.drawnow(drawmoney,userid);
            if(i==1){
                String bankName="";
                String card="";
                String status="已完成";
                String context="支付宝提现";
                String remark="222";
                /*添加提现记录*/
                moneyManageService.addDrawRecoed(new Date(),drawNowVo.getDrawmoney(),card,bankName,status,context,remark,userid);
                return "提现成功";
            }else{
                return "账户余额不足";
            }
        }
    }

    /*银行卡提现*/
    @RequestMapping("/draw/cardDraw")
    public String cardDraw(@RequestBody CardDrawVo cardDrawVo,HttpServletRequest request){
        String token = request.getHeader("token");
        int userid = Integer.parseInt(token);
        /*获取支付密码*/
        String paypwd=moneyManageService.findPaypwdByUserid(userid);
        if(!cardDrawVo.getPaypwd().equals(paypwd)){
            return "密码错误！";
        }else {
            Long drawmoney = cardDrawVo.getDrawmoney();
            /*更新账户余额*/
            int i=moneyManageService.drawnow(drawmoney,userid);
            if(i==1){
                /*余额足够的情况：根据userid和银行卡id获取银行名*/
                String bankName=moneyManageService.findBanknameBycardIdandUserid(cardDrawVo.getCardId(),userid);
                String status="已完成";
                String context="银行卡提现";
                String remark="111";
                /*添加提现记录*/
                moneyManageService.addDrawRecoed(new Date(),cardDrawVo.getDrawmoney(),cardDrawVo.getCardId(),bankName,status,context,remark,userid);
                return "提现成功";
            }else{
                return "账户余额不足";
            }
        }
    }

    /*获取银行卡下拉列表数据*/
    @RequestMapping("/draw/getbanklist")
    public List<UserBank> getbanklist(HttpServletRequest request){
        String token = request.getHeader("token");
        int userid = Integer.parseInt(token);
        List<UserBank> userBanks=moneyManageService.getbanklist(userid);
        return userBanks;
    }

    /*查询提现记录*/
    @RequestMapping("/draw/findAll")
    public PageInfo<DrawVo> findALLdraws(HttpServletRequest request){
        String token = request.getHeader("token");
        int userid = Integer.parseInt(token);
        PageInfo<DrawVo> drawVoPageInfo=moneyManageService.findalldrawsByuserid(userid);
        return drawVoPageInfo;
    }

    /*条件查询提现记录*/
    @RequestMapping("/draw/finddrawbycons")
    public PageInfo<DrawVo> finddrawbycons(@RequestBody FindDrawByConsVo findDrawByConsVo,HttpServletRequest request) throws ParseException {
        String token = request.getHeader("token");
        int userid = Integer.parseInt(token);
        String drawcontext = findDrawByConsVo.getDrawcontext();
        Date starttime=null;
        Date endtime=null;
        if(findDrawByConsVo.getDrawtime()!=null){
            String starttime_s = findDrawByConsVo.getDrawtime()[0];
            String endtime_s = findDrawByConsVo.getDrawtime()[1];
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            starttime = simpleDateFormat.parse(starttime_s);
            endtime = simpleDateFormat.parse(endtime_s);
        }
        Integer start = findDrawByConsVo.getStartPage();
        if(start==null||"".equals(start)){
            start=1;
        }
        int size=4;
        PageInfo<DrawVo> drawVoPageInfo=moneyManageService.finddrawbycons(drawcontext,starttime,endtime,start,size,userid);
        return drawVoPageInfo;
    }

    /*删除提现记录并进行条件查询*/
    @RequestMapping("/draw/deletedraw")
    public PageInfo<DrawVo> deletedrawbyid(@RequestBody DeleteDrawVo deleteDrawVo,HttpServletRequest request) throws ParseException {
        String token = request.getHeader("token");
        int userid = Integer.parseInt(token);
        Integer drawId = deleteDrawVo.getDrawId();
        String drawcontext = deleteDrawVo.getDrawcontext();
        Date starttime=null;
        Date endtime=null;
        if(deleteDrawVo.getDrawtime()!=null){
            String starttime_s = deleteDrawVo.getDrawtime()[0];
            String endtime_s = deleteDrawVo.getDrawtime()[1];
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            starttime = simpleDateFormat.parse(starttime_s);
            endtime = simpleDateFormat.parse(endtime_s);
        }
        Integer start = 1;
        int size=4;
        PageInfo<DrawVo> drawVoPageInfo= moneyManageService.deletedrawbyid(drawId,drawcontext,starttime,endtime,start,size,userid);
        return drawVoPageInfo;
    }
}
